package com.mspbots.core.ticketai;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mspbots.core.ticketai.model.TicketCustom;
import com.mspbots.core.ticketai.model.TicketRule;
import com.mspbots.core.ticketai.service.TicketRuleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.Callable;

/**
 * com.mspbots.core.ticketai.TicketRuleController
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/11/12
 */
@Log4j2
@RestController
@RequiredArgsConstructor
@RequestMapping("ticket/rules")
public class TicketRuleController {

    private final TicketRuleService ruleService;

    @GetMapping("tenant/board")
    public Callable getTenantBoard(@RequestParam Long tenantId, @RequestParam(required = false) String board,
                                   @RequestParam(required = false) String name) {
        Assert.notNull(tenantId, "Property or field 'tenantId' cannot be found on null");
        return () -> this.ruleService.getRules(tenantId, board, name);
    }

    @GetMapping("list")
    public Callable list(@RequestParam Long tenantId, @RequestParam Integer board) {
        Assert.notNull(tenantId, "Property or field 'tenantId' cannot be found on null");
        Assert.notNull(board, "Property or field 'board' cannot be found on null");
        return () -> this.ruleService.getBoardRules(tenantId, board);
    }

    @GetMapping("rulecount")
    public Callable ruleCount(@RequestParam Long tenantId) {
        Assert.notNull(tenantId, "Property or field 'tenantId' cannot be found on null");
        return () -> this.ruleService.getRulesCount(tenantId);
    }

    @GetMapping("page")
    public Callable page(Page<TicketRule> page, TicketRule queryParams) {
        Assert.notNull(queryParams.getTenantId(), "Get params tenantId not null.");
        Assert.notNull(queryParams.getBoard(), "Get params board not null.");
        return () -> this.ruleService.page(page, Wrappers.<TicketRule>lambdaQuery(queryParams).orderByAsc(TicketRule::getName));
    }

    @PostMapping
    public Callable saveAll(@RequestBody List<TicketRule> ticketRules) {
        return () -> this.ruleService.saveBatchAll(ticketRules);
    }

    @GetMapping("custom")
    public Callable custom() {
        return () -> TicketCustom.ticketFildlesJsonObject;
    }

    @GetMapping("repair/status")
    public Callable repairStatus(@RequestParam(required = false) Long tenantId) {
        return () -> this.ruleService.repairStatus(tenantId);
    }

    @GetMapping("init/status")
    public Callable initStatus(@RequestParam(required = false) Long tenantId) {
        log.info("ticket/rules/init/status connected.");
        return () -> this.ruleService.initStatus(tenantId);
    }
}
